
LyLock_Soulstone = { }
table.insert(LyLock_Plugins, LyLock_Soulstone)
LyLock_Soulstone.Frame = "LyLockSoulstoneFrame"
LyLock_Soulstone.Button = "LyLockSoulstoneButton"
LyLock_Soulstone.Events = {
    "PARTY_MEMBERS_CHANGED",
    "PARTY_MEMBER_DISABLE",
    "PARTY_MEMBER_ENABLE",
    "PLAYER_AURAS_CHANGED",

    "RAID_ROSTER_UPDATE",
    
    "UNIT_AURA",
    "PLAYER_ENTERING_WORLD",
    
    "UNIT_SPELLCAST_SENT",
    "UNIT_SPELLCAST_START",
    "UNIT_SPELLCAST_STOP",
    "UNIT_SPELLCAST_FAILED",
    "UNIT_SPELLCAST_INTERRUPTED",
    
    "BAG_UPDATE",
    "BAG_UPDATE_COOLDOWN"
}




------------------------------------------------------
-- Soulsone Settings ---------------------------------
------------------------------------------------------
LyLock_Defaults.Soulstone_Message1 = "Casting Soulstone on %target%";
LyLock_Defaults.Soulstone_Failed1 = "Failed to Soulstone %target%";
LyLock_Defaults.Soulstone_Message2 = "FYI: You have been soulstoned";
LyLock_Defaults.Soulstone_Message3 = "FYI: %target% has been soulstoned";

LyLock_Soulstone.OnSettings = function(action)
    if action=="default" then
 
        LyLock_Settings.Soulstone_Button = ReturnSetting(LyLock_Settings.Soulstone_Button, true)
        LyLock_Settings.Soulstone_Tooltip = ReturnSetting(LyLock_Settings.Soulstone_Tooltip, true)
        LyLock_Settings.Soulstone_MsgScreen = ReturnSetting(LyLock_Settings.Soulstone_MsgScreen, true)
        LyLock_Settings.Soulstone_PVPMode = ReturnSetting(LyLock_Settings.Soulstone_PVPMode, true)
        LyLock_Settings.Soulstone_Cooldown = ReturnSetting(LyLock_Settings.Soulstone_Cooldown, true)
        LyLock_Settings.Soulstone_Unbuffed = ReturnSetting(LyLock_Settings.Soulstone_Unbuffed, true)
        LyLock_Settings.Soulstone_WarnSelf = ReturnSetting(LyLock_Settings.Soulstone_WarnSelf, false)
        LyLock_Settings.Soulstone_Interval = ReturnSetting(LyLock_Settings.Soulstone_Interval, 5)
    
        LyLock_Settings.Soulstone_Casting = ReturnSetting(LyLock_Settings.Soulstone_Casting, true)
        LyLock_Settings.Soulstone_CastFail = ReturnSetting(LyLock_Settings.Soulstone_CastFail, false)
        LyLock_Settings.Soulstone_Whisper = ReturnSetting(LyLock_Settings.Soulstone_Whisper, true)
        LyLock_Settings.Soulstone_Warlocks = ReturnSetting(LyLock_Settings.Soulstone_Warlocks, true)
        
        LyLock_Settings.Soulstone_Message1 = ReturnSetting(LyLock_Settings.Soulstone_Message1, LyLock_Defaults.Soulstone_Message1)
        LyLock_Settings.Soulstone_Failed1 = ReturnSetting(LyLock_Settings.Soulstone_Failed1, LyLock_Defaults.Soulstone_Failed1)
        LyLock_Settings.Soulstone_Message2 = ReturnSetting(LyLock_Settings.Soulstone_Message2, LyLock_Defaults.Soulstone_Message2)
        LyLock_Settings.Soulstone_Message3 = ReturnSetting(LyLock_Settings.Soulstone_Message3, LyLock_Defaults.Soulstone_Message3)

        
    elseif action=="save" then
    
        LyLock_Settings.Soulstone_Button = iif(Soulstone_Button:GetChecked(), true, false);
        LyLock_Settings.Soulstone_Tooltip = iif(Soulstone_Tooltip:GetChecked(), true, false);
        LyLock_Settings.Soulstone_MsgScreen = iif(Soulstone_MsgScreen:GetChecked(), true, false);
        LyLock_Settings.Soulstone_PVPMode = iif(Soulstone_PVPMode:GetChecked(), true, false);
        LyLock_Settings.Soulstone_Cooldown = iif(Soulstone_Cooldown:GetChecked(), true, false);
        LyLock_Settings.Soulstone_Unbuffed = iif(Soulstone_Unbuffed:GetChecked(), true, false);
        LyLock_Settings.Soulstone_WarnSelf = iif(Soulstone_WarnSelf:GetChecked(), true, false);
        LyLock_Settings.Soulstone_Casting = iif(Soulstone_Casting:GetChecked(), true, false);
        LyLock_Settings.Soulstone_CastFail = iif(Soulstone_CastFail:GetChecked(), true, false);
        LyLock_Settings.Soulstone_Whisper = iif(Soulstone_Whisper:GetChecked(), true, false);
        LyLock_Settings.Soulstone_Warlocks = iif(Soulstone_Warlocks:GetChecked(), true, false);
        
        LyLock_Settings.Soulstone_Message1 = ReturnSetting(Soulstone_Message1:GetText(), LyLock_Defaults.Soulstone_Message1);
        LyLock_Settings.Soulstone_Failed1 = ReturnSetting(Soulstone_Failed1:GetText(), LyLock_Defaults.Soulstone_Failed1);
        LyLock_Settings.Soulstone_Message2 = ReturnSetting(Soulstone_Message2:GetText(), LyLock_Defaults.Soulstone_Message2);
        LyLock_Settings.Soulstone_Message3 = ReturnSetting(Soulstone_Message3:GetText(), LyLock_Defaults.Soulstone_Message3);
        
    elseif action=="load" then
    

        Soulstone_Button:SetChecked(LyLock_Settings.Soulstone_Button);
        Soulstone_Tooltip:SetChecked(LyLock_Settings.Soulstone_Tooltip);
        Soulstone_MsgScreen:SetChecked(LyLock_Settings.Soulstone_MsgScreen);
        Soulstone_PVPMode:SetChecked(LyLock_Settings.Soulstone_PVPMode);
        Soulstone_Cooldown:SetChecked(LyLock_Settings.Soulstone_Cooldown);
        Soulstone_Unbuffed:SetChecked(LyLock_Settings.Soulstone_Unbuffed);
        Soulstone_WarnSelf:SetChecked(LyLock_Settings.Soulstone_WarnSelf);
        Soulstone_Casting:SetChecked(LyLock_Settings.Soulstone_Casting);
        Soulstone_Whisper:SetChecked(LyLock_Settings.Soulstone_Whisper);
        Soulstone_Warlocks:SetChecked(LyLock_Settings.Soulstone_Warlocks);

        Soulstone_Message1:SetText(LyLock_Settings.Soulstone_Message1);
        Soulstone_Failed1:SetText(LyLock_Settings.Soulstone_Failed1);
        Soulstone_Message2:SetText(LyLock_Settings.Soulstone_Message2);
        Soulstone_Message3:SetText(LyLock_Settings.Soulstone_Message3);
        
    end
end


--------------------------------------------------------------------------------------
-- Soulstone Functions ---------------------------------------------------------------
--------------------------------------------------------------------------------------
LyLock_Soulstone.Item = nil
LyLock_Soulstone.Group = { "player", 1, 1 }

LyLock_Soulstone.PVPMode = false
LyLock_Soulstone.InfoPlayers = ""
LyLock_Soulstone.InfoCooldown = ""
LyLock_Soulstone.ToolPlayers = ""
LyLock_Soulstone.ToolCooldown = ""

LyLock_Soulstone.Cooldown = 0
LyLock_Soulstone.Notified = false

LyLock_Soulstone.Started=0
LyLock_Soulstone.Message=0
LyLock_Soulstone.Progress=0
LyLock_Soulstone.Player=nil

LyLock_Soulstone.Names={}


LyLock_Soulstone.GetCooldown = function()
    return (900-(GetTime()-LyLock_Soulstone.Started))
end

LyLock_Soulstone.ButtonSetupAction = function()
    LyLock_SetupActionStone(LyLockSoulstoneButton, WORD_SOULSTONE, WORD_CREATE_SOULSTONE, WORD_ITEM_SOULSTONE)
end


LyLock_Soulstone.ButtonOnMouseDown = function()
    if LyLock_Soulstone.Item and (not IsShiftKeyDown()) then
        if (LyLock_Soulstone.Started>0) or (UnitIsDeadOrGhost("player")==1) then
            LyLockSoulstoneButton:Disable()
            if (LyLock_Soulstone.Started>0) then
                EchoMessage("LyLock", "Soulstone cooldown is "..strtime(LyLock_Soulstone.GetCooldown()), "Cooldown")
            else
                EchoMessage("LyLock", "Soulstone is ready to be used", "ReadyUse")
            end
        else
            if (UnitIsPlayer("target") and (UnitInParty("target") or UnitInRaid("target"))) then
                if CheckInteractDistance("target", 4) then
                    LyLockSoulstoneButton:Enable()
                else
                    LyLockSoulstoneButton:Disable()
                    EchoMessage("LyLock", "You are too far away to Soulstone that player", "Canceled")
                end
            else
                LyLockSoulstoneButton:Disable()
                EchoMessage("LyLock", "Target a player in the raid or party to Soulstone", "Canceled")
            end
        end
    elseif not IsShiftKeyDown() and not IsCtrlKeyDown() then
        LyLockSoulstoneButton:Disable()
        EchoMessage("LyLock", "You do not have a Soulstone created", "Canceled")
    elseif IsShiftKeyDown() then
        LyLockSoulstoneButton:Enable()
    else
        -- Cast ritual of souls here... but there's more than one rank? argh
    end
end

LyLock_Soulstone.ButtonTooltipText = function()
    if LyLock_Settings.Soulstone_Tooltip then
        return LyLock_Soulstone.ToolPlayers..LyLock_Soulstone.ToolCooldown.."\n"..COLOR_BRIGHTGREEN.."Click to Use Stone\nShift-Click to Create\nCtrl-Click to Summon Ritual\n"
    end
end

LyLock_Soulstone.OnLoad = function()
    LyLock_Soulstone.UpdateItem()

    UpdateSoulstoneGroup()       
    UpdateSoulstoneNames()
    UpdateSoulstoneStates()
    
end
LyLock_Soulstone.OnEvent = function()
    if event=="BAG_UPDATE" then
        LyLock_Soulstone.UpdateItem()
    elseif event=="BAG_UPDATE_COOLDOWN" then
        LyLock_Soulstone.UpdateItem()
    elseif event=="UNIT_SPELLCAST_SENT" then
        if arg1=="player" and string.find(arg2, WORD_SOULSTONERES) then
            LyLock_Soulstone.Progress = 1
            LyLock_Soulstone.Player = arg4
        end
    elseif event=="UNIT_SPELLCAST_START" then
        if arg1=="player" and (LyLock_Soulstone.Progress==1) then
            LyLock_Soulstone.Progress = 2
            if LyLock_Settings.Soulstone_Casting and (not LyLock_Soulstone.PVPMode) then
                Speak(LyMsg(LyLock_Settings.Soulstone_Message1, LyLock_Soulstone.Player))
            else
                EchoMessage("LyLock", LyMsg(LyLock_Settings.Soulstone_Message1, LyLock_Soulstone.Player), "Casting")
            end
            
        end
    elseif event=="UNIT_SPELLCAST_STOP" then
        if arg1=="player" and (LyLock_Soulstone.Progress == 2) then
            LyLock_Soulstone.Progress = 3
        end
    elseif event=="UNIT_SPELLCAST_INTERRUPTED" then
        if arg1=="player" and (LyLock_Soulstone.Progress == 3) then
            LyLock_Soulstone.Progress = 0
            if not IsPlayerSoulstoned(LyLock_Soulstone.Player) then
                if LyLock_Settings.Soulstone_CastFail and (LyLock_Soulstone.Player ~= LyLock_PlayerName) and (not LyLock_Soulstone.PVPMode) then
                    Speak(LyMsg(LyLock_Settings.Soulstone_Failed1, LyLock_Soulstone.Player))
                else
                    EchoMessage("LyLock", "Failed to Soulstone "..LyLock_Soulstone.Player, "Canceled")
                end
            end
            LyLock_Soulstone.Player = nil
        end
    elseif event=="PARTY_MEMBERS_CHANGED" or event=="RAID_ROSTER_UPDATE" or event=="PARTY_MEMBER_DISABLE" or event=="PARTY_MEMBER_ENABLE" then
        UpdateSoulstoneGroup()       
        UpdateSoulstoneNames()
        UpdateSoulstoneStates("")
    elseif event=="UNIT_AURA" then
    
            UpdateSoulstoneGroup()       
        UpdateSoulstoneNames()
        UpdateSoulstoneStates("")
        
--~         EchoEvent()
--~         UpdateSoulstoneStates(UnitName(arg1))
    elseif event=="PLAYER_AURAS_CHANGED" then
                UpdateSoulstoneGroup()       
        UpdateSoulstoneNames()
        UpdateSoulstoneStates("")
        
--~         EchoEvent()
--~         UpdateSoulstoneStates(LyLock_PlayerName)
    elseif event=="PLAYER_ENTERING_WORLD" then
                UpdateSoulstoneGroup()       
        UpdateSoulstoneNames()
        UpdateSoulstoneStates("")
        
--~         EchoEvent()
--~         UpdateSoulstoneGroup()
    end
end
LyLock_Soulstone.UpdatePVP = function()
    if LyLock_Settings.Soulstone_PVPMode then
        LyLock_Soulstone.PVPMode = (UnitIsPVP("player")~=nil)
    else
        LyLock_Soulstone.PVPMode = false
    end
end
LyLock_Soulstone.UpdateItem = function()
    LyLock_Soulstone.Item = FindContainerItemByName(WORD_SOULSTONE)
    if (LyLock_Soulstone.Item) then
        local za, zb, zc=GetContainerItemCooldown(LyLock_Soulstone.Item[1], LyLock_Soulstone.Item[2]);
        LyLock_Soulstone.Started = iif((za>0), za, 0)
    end
end
LyLock_Soulstone.OnHalfSecond = function()

	if (LyLock_Soulstone.Player~=nil) and (LyLock_Soulstone.Progress==3) then
		if IsPlayerSoulstoned(LyLock_Soulstone.Player) then
            UpdateSoulstoneStates(LyLock_Soulstone.Player)
            LyLock_Soulstone.Progress = 0
			LyLock_Soulstone.Started = GetTime()
			if LyLock_Settings.Soulstone_Whisper then
				WhisperName(LyLock_Soulstone.Player, LyMsg(LyLock_Settings.Soulstone_Message2, LyLock_Soulstone.Player))
			end
			if LyLock_Settings.Soulstone_Warlocks and (not LyLock_Soulstone.PVPMode) then
				WhisperWarlocks(LyLock_Soulstone.Player, LyMsg(LyLock_Settings.Soulstone_Message3, LyLock_Soulstone.Player))
			end
			LyLock_Soulstone.Player = nil
		end
	end
    
end

LyLock_Soulstone.OnSecond = function()
    
    

    local cdTime = LyLock_Soulstone.GetCooldown()
    if (LyLock_Soulstone.Started>0) and (cdTime>=0) then
        LyLockSoulstoneCooldownText:SetText(strtimeM(cdTime));
        LyLock_Soulstone.InfoCooldown = "Cooldown: "..strtimeMin(cdTime)
        LyLock_Soulstone.ToolCooldown = COLOR_LIGHTORANGE.."Cooldown: "..COLOR_WHITE..strtimeMin(cdTime).."\n"
        if LyLock_Settings.Soulstone_Cooldown then
            if (LyLock_Soulstone.Cooldown>=1619) and (LyLock_Soulstone.Cooldown<1620) then
                EchoMessage("LyLock", "Soulstone will be ready in 3 Mins", "Cooldown")
            elseif (LyLock_Soulstone.Cooldown>=1679) and (LyLock_Soulstone.Cooldown<1680) then
                EchoMessage("LyLock", "Soulstone will be ready in 2 Mins", "Cooldown")
            elseif (LyLock_Soulstone.Cooldown>=1739) and (LyLock_Soulstone.Cooldown<1740) then
                EchoMessage("LyLock", "Soulstone will be ready in 1 Min", "Cooldown")
            elseif (LyLock_Soulstone.Cooldown>=1799) and (LyLock_Soulstone.Cooldown<1800) then
                EchoMessage("LyLock", "Soulstone is now ready to cast", "ReadyUse")
                LyLock_Soulstone.Notified = true
            end
        end
    else
        LyLock_Soulstone.Started = 0
        LyLockSoulstoneCooldownText:SetText(iif(LyLock_Soulstone.Item, LYLOCK_FLAGCAST, LYLOCK_FLAGMAKE));
        LyLock_Soulstone.InfoCooldown = "Cooldown: Ready"
        LyLock_Soulstone.ToolCooldown = COLOR_LIGHTORANGE.."Cooldown: "..COLOR_WHITE.."Ready".."\n"
    end
    
    
	if (LyLock_Soulstone.Cooldown<=0) and (UnitIsDeadOrGhost("player")==nil) and (table.getn(LyLock_Soulstone.Names)==0) then
        if ((GetTime()-LyLock_Soulstone.Message)>=(LyLock_Settings.Soulstone_Interval*60)) or (LyLock_Soulstone.Message==0) then
            if LyLock_Soulstone.Group[1]~="player" and LyLock_Settings.Soulstone_Unbuffed and (not LyLock_Soulstone.PVPMode) then
                LyLock_Soulstone.Message = GetTime()
                if not LyLock_Soulstone.Notified then
                    if LyLock_Settings.Soulstone_MsgScreen then
                        ScreenMessage("Soulstone is not on any player in your group")
                    end
                    EchoMessage("LyLock", "Soulstone is not on any player in your group", "UnBuffed")
                else
                    LyLock_Soulstone.Notified=false
                end
            elseif (LyLock_Soulstone.Group[1]=="player") and LyLock_Settings.Soulstone_WarnSelf then
                LyLock_Soulstone.Message = GetTime()
                if not LyLock_Soulstone.Notified then
                    if LyLock_Settings.Soulstone_MsgScreen then
                        ScreenMessage("Soulstone ... look into it")
                    end
                    EchoMessage("LyLock", "Soulstone ... look into it", "UnBuffed")
                else
                    LyLock_Soulstone.Notified=false
                end
            else
                LyLock_Soulstone.Message = 0
                LyLock_Soulstone.Notified=false
            end
        end
	else
		LyLock_Soulstone.Message = 0
        LyLock_Soulstone.Notified=false
	end
    
    
end


function UpdateSoulstoneGroup()
    if LyLock_Settings.Soulstone_PVPMode then
        LyLock_Soulstone.PVPMode = (UnitIsPVP("player")~=nil)
    else
        LyLock_Soulstone.PVPMode = false
    end
    if (GetNumRaidMembers()>0) then
        if (LyLock_Soulstone.Group[1]~="raid") then
            LyLock_Soulstone.Message = 0
            LyLock_Soulstone.Notified=false
        end
        LyLock_Soulstone.Group = { "raid", GetNumRaidMembers(),  MAX_RAID_MEMBERS }
    elseif (GetNumPartyMembers()>0) then
        if (LyLock_Soulstone.Group[1]~="party") then
            LyLock_Soulstone.Message = 0
            LyLock_Soulstone.Notified=false
        end
        LyLock_Soulstone.Group =  { "party", GetNumPartyMembers(),  MAX_PARTY_MEMBERS }
    elseif (LyLock_Soulstone.Group[1]~="player") then
        LyLock_Soulstone.Message = 0
        LyLock_Soulstone.Notified=false
        LyLock_Soulstone.Group = { "player", 1, 1 }
        LyLock_Soulstone.Names = { }
    end
end
function UpdateSoulstoneNames()

    local ret = nil
	if (LyLock_Soulstone.Group[1]~="player") then
		for ii=1, LyLock_Soulstone.Group[3]  do
			local name = UnitName(LyLock_Soulstone.Group[1]..ii)
			if name~=nil and name~=LyLock_PlayerName then
                ret = IndexByName(LyLock_Soulstone.Names, name)
                if ret==nil then
                    if IsUnitSoulstoned(LyLock_Soulstone.Group[1]..ii) then
                        table.insert(LyLock_Soulstone.Names, { Name = name, Unit = LyLock_Soulstone.Group[1]..ii, State = 1 })
                    end
                else
                    LyLock_Soulstone.Names[ret].Unit = LyLock_Soulstone.Group[1]..ii;
				end
			end
		end
	end
	if IsUnitSoulstoned("player") then
        ret = IndexByName(LyLock_Soulstone.Names, LyLock_PlayerName)
        if ret==nil then
            if IsUnitSoulstoned("player") then
                table.insert(LyLock_Soulstone.Names, { Name = LyLock_PlayerName, Unit = "player", State = 1 })
            end
        else
            LyLock_Soulstone.Names[ret].Unit = "player";
        end
	end
end

function UpdateSoulstoneStates(name)
    local ssDel = {}
    if table.getn(LyLock_Soulstone.Names) > 0 then
        local i = next(LyLock_Soulstone.Names)
        while (i~=nil) do
            --if (LyLock_Soulstone.Names[i].Name==name) or (name=="") then
                if (UnitIsConnected(LyLock_Soulstone.Names[i].Unit)==nil) then
                    if (LyLock_Soulstone.Names[i].State ~= 3) then -- and (LyLock_Soulstone.Names[i].State == 1) then
                        LyLock_Soulstone.Names[i].State = 3
                        LyLock_Soulstone.Notified = true
                        if (not LyLock_Soulstone.PVPMode) then
                            if LyLock_Settings.Soulstone_MsgScreen then
                                ScreenMessage("Soulstoned player "..LyLock_Soulstone.Names[i].Name.." has disconnected");
                            end
                            EchoMessage("LyLock", "Soulstoned player "..LyLock_Soulstone.Names[i].Name.." has disconnected", "UnBuffed")
                        end
                    end
                elseif (UnitIsDeadOrGhost(LyLock_Soulstone.Names[i].Unit)~=nil) then
                    if (LyLock_Soulstone.Names[i].State ~= 2) then -- and (LyLock_Soulstone.Names[i].State == 1) then
                        LyLock_Soulstone.Names[i].State = 2
                        LyLock_Soulstone.Notified = true
                        if (not LyLock_Soulstone.PVPMode) then
                            if LyLock_Settings.Soulstone_MsgScreen then
                                ScreenMessage("Soulstoned player "..LyLock_Soulstone.Names[i].Name.." has died");
                            end
                            EchoMessage("LyLock", "Soulstoned player "..LyLock_Soulstone.Names[i].Name.." has died", "UnBuffed")
                        end
                    end
                elseif not IsUnitSoulstoned(LyLock_Soulstone.Names[i].Unit) then
                    if (LyLock_Soulstone.Names[i].State ~= 0) then
                        local tmpdo = 1
                        
                        if (UnitClass(LyLock_Soulstone.Names[i].Unit)==WORD_PRIEST) then
                            if ((UnitMana(LyLock_Soulstone.Names[i].Unit) == UnitManaMax(LyLock_Soulstone.Names[i].Unit)) and (UnitHealth(LyLock_Soulstone.Names[i].Unit) == UnitHealthMax(LyLock_Soulstone.Names[i].Unit))) then
                                if (UnitAffectingCombat(LyLock_Soulstone.Names[i].Unit)~=nil) then
                                    tmpdo = 0
                                end
                            end
                        end
                        
                        if (tmpdo==1) then
                            LyLock_Soulstone.Names[i].State = 0
                            
                            if (LyLock_Soulstone.Names[i].State == 1) then
                                LyLock_Soulstone.Notified = true
                                if (not LyLock_Soulstone.PVPMode) then
                                    if LyLock_Settings.Soulstone_MsgScreen then
                                        ScreenMessage("Soulstoned has faded from "..LyLock_Soulstone.Names[i].Name);
                                    end
                                    EchoMessage("LyLock", "Soulstoned has faded from "..LyLock_Soulstone.Names[i].Name, "UnBuffed")
                                end
                            end

                            table.insert(ssDel, i)
                        end

                    end
                else
                    LyLock_Soulstone.Names[i].State = 1
                end 
            --end
            i = next(LyLock_Soulstone.Names, i)
        end
    end

    
    local ssInfo = "";
    local ssInfo2 = "";
    
    if table.getn(LyLock_Soulstone.Names)>0 then
        local i = next(LyLock_Soulstone.Names)
        while (i~=nil) do
            if (LyLock_Soulstone.Names[i].State == 2) then
                ssInfo = LyLock_Soulstone.Names[i].Name.."(Dead)"..iif((i==1),". ", ", ")..ssInfo
                ssInfo2 = LyLock_Soulstone.Names[i].Name.."(Dead)"..iif((i>1),"\n                     ", "")..ssInfo2
            elseif (LyLock_Soulstone.Names[i].State==3) then
                ssInfo = LyLock_Soulstone.Names[i].Name.."(DC)"..iif((i==1),". ", ", ")..ssInfo
                ssInfo2 = LyLock_Soulstone.Names[i].Name.."(DC)"..iif((i>1),"\n                     ", "")..ssInfo2
            else
                ssInfo = LyLock_Soulstone.Names[i].Name..iif((i==1),". ", ", ")..ssInfo
                ssInfo2 = LyLock_Soulstone.Names[i].Name..iif((i>1),"\n                     ", "")..ssInfo2
            end

            i = next(LyLock_Soulstone.Names, i)
        end
        ssInfo = "Soulstoned: "..ssInfo
        --ssInfo2 = COLOR_LIGHTORANGE.."Soulstoned:"..iif(string.find(ssInfo2,"\n"), "\n                    ", " ")..COLOR_WHITE..ssInfo2.."\n"
        ssInfo2 = COLOR_LIGHTORANGE.."Soulstoned:".." "..COLOR_WHITE..ssInfo2.."\n"..iif(string.find(ssInfo2,"\n"), "\n", "")
    else
        ssInfo = "Soulstoned: (Nobody)."
        ssInfo2 = COLOR_LIGHTORANGE.."Soulstoned: "..COLOR_WHITE.."(Nobody)\n"
    end	
    LyLock_Soulstone.InfoPlayers = ssInfo;
    LyLock_Soulstone.ToolPlayers = ssInfo2;

    i = next(ssDel)
    while (i~=nil) do
        
        table.remove(LyLock_Soulstone.Names, ssDel[i])
            
        i = next(ssDel, i)
    end  
end

function IsPlayerSoulstoned(player)
	if UnitName("target") == player then
		return IsUnitSoulstoned("target")
	elseif LyLock_PlayerName==player then
		return IsUnitSoulstoned("player")
	elseif (LyLock_Soulstone.Group[1]~="player") then
		for ii=1, LyLock_Soulstone.Group[3] do
			local name = UnitName(LyLock_Soulstone.Group[1]..ii)
			if name~=nil and player==name then
				if IsUnitSoulstoned(LyLock_Soulstone.Group[1]..ii) then
					return true
				end
			end
		end
	end
end






function IsUnitSoulstoned(unit)
	return IsUnitBuff(unit, TEXTURE_SOULSTONE)
end



